package com.chezhujie.www.controller;

import com.chezhujie.www.domain.Classify;
import com.chezhujie.www.service.ClassifyService;
import com.xy.core.page.Pagination;
import com.xy.core.utils.ConvertObj;
import com.xy.core.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 *   店铺信息
 * Created by 汤伟杰 on 2017/5/23.
 */
@Controller
@RequestMapping("/classify")
public class ClassifyController {
    @Autowired
    private ClassifyService classifyService;

    /** 列表 **/
    @RequestMapping("/list.do")
    public String list(Integer pageNo, Integer pageSize, Classify classify, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(classify,req,model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(classify);
            Pagination pagination = classifyService.list(pageNo.intValue(), pageSize.intValue(), params, model);
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取店铺信息列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", classify);
        return "classify/list";
    }
    /** 保存 **/
    @RequestMapping("/save.do")
    public String save(Classify classify,
                      HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        try {

            if(StringUtils.isNotBlank(classify.getClassifyId())){
                classifyService.update(classify);


            }else{
                classify.setClassifyId(UUIDUtils.getUUID());
                classifyService.insert(classify);
            }
            model.addAttribute("success", "保存店铺信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "保存店铺信息失败!详情："+e.getMessage());
        }
        return "redirect:list.do";
    }


    /** 详情 **/
    @RequestMapping("/detail.do")
    public String detail(Classify classify, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(classify,req,model);
        try {
            Classify entity = classifyService.queryById(classify.getClassifyId());
            model.addAttribute("entity", entity);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "查询店铺信息出错!详情："+e.getMessage());
        }
        return "classify/detail";
    }
    /** 新增或修改 **/
    @RequestMapping("/add.do")
    public String add(Classify classify, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(classify,req,model);
        if(StringUtils.isNotBlank(classify.getClassifyId())){
            classify = classifyService.queryById(classify.getClassifyId());
            model.addAttribute("entity", classify);
        }
        return "classify/add";
    }
    /** 删除 **/
    @RequestMapping("/delete.do")
    public String delete(Classify classify, HttpServletRequest req, HttpServletResponse resp,
                     ModelMap model){
        try {
            classifyService.deleteById(classify.getClassifyId());
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "redirect:list.do";
    }

    private void setMsg(Classify classify,HttpServletRequest req, ModelMap model) {
        model.addAttribute("failed", req.getParameter("failed"));
        model.addAttribute("success", req.getParameter("success"));
        model.addAttribute("entity", classify);
    }
}
